﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace AnalyseServer
{
    public partial class MainForm : Form
    {
        EneterDataServer dataServer = new EneterDataServer();
        public MainForm()
        {
            InitializeComponent();

            //dataServer.OnDataReceived += new EventHandler(OnDataReceived);
            dataServer.OnDataReceived += OnDataReceived;
            dataServer.StartServer();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //PushData<float> test = new PushData<float>(30);

            //Random rand = new Random();
            //for (int i = 0; i < 30; i++)
            //{
            //    float val = (float)(5 * rand.NextDouble());
            //    test.Add(val);
            //}

            //ucChartSensor1.ApplyChartValue(test.Data);

            ucChartSensorAccX.SetChartTitle("Chart Accelerometer X");
            ucChartSensorAccX.SetMaxValue(40);

            ucChartSensorAccY.SetChartTitle("Chart Accelerometer Y");
            ucChartSensorAccY.SetMaxValue(40);

            ucChartSensorAccZ.SetChartTitle("Chart Accelerometer Z");
            ucChartSensorAccZ.SetMaxValue(40);
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            dataServer.StopServer();
        }

        private void OnDataReceived(object sender, EventArgs e)
        {
            byte[] buffer = (byte[])sender;
            float x, y, z;
            using (MemoryStream stream = new MemoryStream(buffer))
            {
                using (BinaryReader binReader = new BinaryReader(stream))
                {
                    x = NumericUtils.ReadSingleBigEndian(binReader);
                    y = NumericUtils.ReadSingleBigEndian(binReader);
                    z = NumericUtils.ReadSingleBigEndian(binReader);
                }
            }

            //String output = String.Format("X: {0:F3}, Y: {1:F3}, Z: {2:F3}", x, y, z);
            //textStatus.Text = output;

            SensorData.accX.Add(x);
            SensorData.accY.Add(y);
            SensorData.accZ.Add(z);


            //ucChartSensorAccX.ApplyChartValue(SensorData.accX.Data);
            object[] objX = new object[1];
            objX[0] = SensorData.accX.Data;
            ucChartSensorAccX.BeginInvoke(new InvokeDelegate(UpdateChartAccX), objX);

            object[] objY = new object[1];
            objY[0] = SensorData.accY.Data;
            ucChartSensorAccY.BeginInvoke(new InvokeDelegate(UpdateChartAccY), objY);

            object[] objZ = new object[1];
            objZ[0] = SensorData.accZ.Data;
            ucChartSensorAccZ.BeginInvoke(new InvokeDelegate(UpdateChartAccZ), objZ);
        }


        public delegate void InvokeDelegate(float[] series);
        public void UpdateChartAccX(float[] series)
        {
            ucChartSensorAccX.ApplyChartValue(SensorData.accX.Data);
        }

        public void UpdateChartAccY(float[] series)
        {
            ucChartSensorAccY.ApplyChartValue(SensorData.accY.Data);
        }

        public void UpdateChartAccZ(float[] series)
        {
            ucChartSensorAccZ.ApplyChartValue(SensorData.accZ.Data);
        }


    }
}
